<?php
class ModelPostsPost extends Model {
	public function add($value) {
		

		$this->db->query("INSERT INTO "  . "wp_posts SET title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "', image = '" . $this->db->escape($value['image']) . "', link = '" . $this->db->escape($value['link']) . "'");

		
		$information_id = $this->db->getLastId();
		return $information_id;
	}

	public function edit($id, $value) {
		$this->db->query("UPDATE "  . "wp_posts SET title = '" . $this->db->escape($value['title']) . "', description = '" . $this->db->escape($value['description']) . "', image = '" . $this->db->escape($value['image']) . "', link = '" . $this->db->escape($value['link']) . "' WHERE id = '" . (int)$id . "'");
	}

	public function delete($id) {
		$this->db->query("DELETE FROM "  . "wp_posts WHERE id = '" . (int)$id . "'");

	}

	public function getItem($id) {
		$query = $this->db->query("SELECT * FROM "  . "wp_posts WHERE id = '" . (int)$id . "'");

		return $query->row;
	}

	public function getList($data = array()) {
		$sql = "SELECT * FROM "  . "wp_posts ";

		$sort_data = array(
			'title'
		);

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];
		} else {
			$sql .= " ORDER BY id";
		}

		if (isset($data['order'])) {
			if($data['order'] == 'DESC')
				$sql .= " DESC";
			else
				$sql .= " ASC";
		} else {
			$sql .= " DESC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}

		$query = $this->db->query($sql);

		return $query->rows;
	}

	
	


	public function getTotal() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM "  . "wp_posts");

		return $query->row['total'];
	}
}